// import { TileLoader } from './3dtiles/Tile3DLoader';
// import { OGC3DTile } from './3dtiles/Tile';
import { TileLoader } from './3dtiles/TileLoader';
import { OGC3DTile } from './3dtiles/OGC3DTile';
import { Matrix4 } from '../../core/math/Matrix4';
import { Engine } from '../../Engine';
import { any } from '../../core/logic';
import { Evented, EventType } from '../../event/index';
import { ComponentType } from '../../component/index';
import { ComponentEvent } from '../../event/ComponentEvent';
/**
 * GLTF加载器类，用于加载GLTF资源
 * @class
 * @memberof w.loader
 * @example
 *  let loader = new w.loader.GLTFLoader({back:true});
 *  loader.load('http://127.0.0.1/demo/assets/testmodel/ljjc.gltf', (gltf) => {
 *   gltf.rotateX((Math.PI * 90) / 180);
 *   gltf.scale = [0.1, 0.1, 0.1];
 *   scene.add(gltf);
 *  });
 */


class W3DTilesLoader extends Evented {
  /**
   * 创建一个GLTF加载器实例。
   * @constructor
   * @param {*} opts
   * @param {Boolean} opts.back 【可选】是否使用子线程加载GLTF,缺省值：true。
   */
  constructor(opts = {}) {
    super()
    this.back = any(opts.back, true);
    this.indexedDB = any(opts.indexedDB, false);
    this.geometricErrorMultiplier = any(opts.geometricErrorMultiplier, 1);
    this.maxCachedItems = any(opts.maxCachedItems, 1000);
    this.centerModel = any(opts.centerModel, true);
    this.translation = [-Engine.instance.center[0], -Engine.instance.center[1], -Engine.instance.center[2]];
    this.crs = any(opts.crs, "4326");
    this.type = any(opts.type, ComponentType.Animation);
    this.event = new ComponentEvent(this.type);
    this.on(EventType.FrameCompleted, this.update);
  }
  /**
   * 加载3dtiles资源。
   * @method
   * @param {String} url 3dtiles资源路径。
   * @param {Function} callback 3dtiles资源加载后回调函数。
   */
  load(url, callback) {
    const rootNode = this.initTileset(url)
    callback(rootNode)
  }
  initTileset(url) {
    let tileLoader = new TileLoader({
      maxCachedItems: this.maxCachedItems,
      geometricErrorMultiplier: this.geometricErrorMultiplier,
      indexedDB: this.indexedDB,
      back: this.back,
      translation: this.translation,
      crs: this.crs
    });
    this.tileLoader = tileLoader
    let ogc3DTile = new OGC3DTile({
      geometricErrorMultiplier: tileLoader.geometricErrorMultiplier,
      url: url,
      loadOutsideView: false,
      tileLoader: tileLoader,
      centerModel: this.centerModel,
      isRoot: true,
      index: 0,
    });
    Engine.instance.scene.addEntity(ogc3DTile.object3D)
    this.ogc3DTile = ogc3DTile
    const camera = Engine.instance.scene.getCamera()
    this.camera = camera
    this.ogc3DTile.update(camera);
    this.tileLoader.update();
    // window.clearInterval(this.timer)
    // this.timer = setInterval(() => {
    //   this.ogc3DTile?.updateCamera(this.camera);
    //   this.tileLoader?.update();
    // }, 30);
    // update(tileLoader,ogc3DTile,camera)
    // function update() {
    //   ogc3DTile.updateCamera(camera);
    //   tileLoader.update();
    //   requestAnimationFrame(update);
    // }
    return ogc3DTile
  }
  update(event) {
    if (event.type === "FrameCompleted") {
      this.ogc3DTile?.update(this.camera);
      this.tileLoader?.update();
      this.event.mode = "value";
      this.publish(this.event);
    }
  }

}
export { W3DTilesLoader };
